package com.example.tests;

import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;

import org.openqa.selenium.*;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;


import com.example.pages.InternalPage;
import com.example.pages.LoginPage;
import com.example.pages.MovieAddPage;
import com.example.pages.MovieListPage;
import com.example.pages.MoviePage;

import ru.esteru.selenium.factory.WebDriverFactory;

public class DeleteMovie {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
private LoginPage loginPage;
private InternalPage internalPage;
private MovieListPage movieListPage;
private MoviePage moviePage;

  @Before
  public void setUp() throws Exception {
	driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testDeleteMovie() throws Exception {
	openloginPage();
	loginPage.loginWithValidCredentials("admin", "admin");
	//assertFalse("No Movie to Remove", driver.findElements(movieListPage.contentMovie).size() >0); doesn't work now
	if (movieListPage.quantityOfMovies() >0){
       deleteMovie();
	}
    logout();
  }

private void logout() {
	internalPage.menuLogoutLink.click();
	//wait.until(visibilityOfElementLocated(By.linkText("Log out"))).click(); remove here as using page objects
	//driver.findElement(By.linkText("Log out")).click(); remove here as wait was added
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
}

private void deleteMovie() {
	movieListPage.movie.click();
    moviePage.buttonRemove.click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$"));
}

private void openloginPage() {
	driver.get(baseUrl + "/php4dvd/");
	loginPage = PageFactory.initElements(driver, LoginPage.class);
	internalPage = PageFactory.initElements(driver, InternalPage.class);
	movieListPage = PageFactory.initElements(driver, MovieListPage.class);
	PageFactory.initElements(driver, MovieAddPage.class);
	moviePage = PageFactory.initElements(driver, MoviePage.class);
}

  @After
  public void tearDown() throws Exception {
   // driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}